﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;

public partial class Register : System.Web.UI.Page
{
    string UserName, Userid, FirstName, LastName, Address;
    int Age;
    long PhoneNumber, CreditCard;
    bool Male;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            Response.Redirect("~/Users/Home.aspx");
        }
    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        MembershipUser NewUser = Membership.GetUser(CreateUserWizard1.UserName);
        UserName = NewUser.UserName;
        Userid = NewUser.ProviderUserKey.ToString();
        Roles.AddUserToRole(UserName, "member");

        ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(NewUser.UserName, true);

        profile.FirstName = FirstName;
        profile.LastName = LastName;
        profile.Age = Age;
        profile.Male = Male;
        profile.Address = Address;
        profile.PhoneNumber = PhoneNumber;
        profile.CreditCard = CreditCard;

        profile.Save();

    }

    private Control CustomeMethod_FindControl(Control _control, String id)
    {
        Control result = null;
        if (_control != null)
        {
            result = _control.FindControl(id);
            if (result != null)
            {
                return result;
            }
            else
            {
                foreach (Control c in _control.Controls)
                {
                    result = CustomeMethod_FindControl(c, id);
                    if (result!=null)
                    {
                        return result;
                    }
                }
            }
        }
        return result;
    }

    protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        try
        {
            TextBox txtFirstName = CustomeMethod_FindControl(CreateUserWizard1, "txtFirstName") as TextBox;
            FirstName = txtFirstName.Text;

            TextBox txtLastName = CustomeMethod_FindControl(CreateUserWizard1, "txtLastName") as TextBox;
            LastName = txtLastName.Text;

            TextBox txtAge = CustomeMethod_FindControl(CreateUserWizard1, "txtAge") as TextBox;
            Age = Convert.ToInt32(txtAge.Text);

            RadioButton rdMale = CustomeMethod_FindControl(CreateUserWizard1, "RadioButton1") as RadioButton;
            Male = rdMale.Checked;

            TextBox txtAddress = CustomeMethod_FindControl(CreateUserWizard1, "txtAddress") as TextBox;
            Address = txtAddress.Text;

            TextBox txtPhone = CustomeMethod_FindControl(CreateUserWizard1, "txtPhone") as TextBox;
            PhoneNumber = Convert.ToInt64(txtPhone.Text);

            TextBox txtCreditCard = CustomeMethod_FindControl(CreateUserWizard1, "txtCreditCard") as TextBox;
            CreditCard = Convert.ToInt64(txtCreditCard.Text);
        }
        catch (Exception)
        {
            e.Cancel = true;
        }

    }

    protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
    {
        string url = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath+ "/VerifyNewUser.aspx?ID=" + Userid;

        e.Message.Body = e.Message.Body.Replace("<%Username%>", UserName);
        e.Message.Body = e.Message.Body.Replace("<%VerifyLink%>", url);

        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;
        client.Send(e.Message);
        e.Cancel = true;
    }
}
